<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="文件名称">
        <el-input
          v-model="form.fileName"
          placeholder="请输入文件名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="search">查 询</el-button>
    <el-button type="primary" @click="upload">上 传</el-button>
    <el-dialog :visible.sync="show" title="上 传">
      <shangChuanVue
        :url="url"
        @shangChuanEvent="shangChuanEvent"
      ></shangChuanVue>
    </el-dialog>
    <el-table :data="tableData">
      <el-table-column prop="name" label="文件名"></el-table-column>
      <el-table-column prop="duration" label="播放时长"></el-table-column>
      <el-table-column prop="操作">
        <template slot-scope="scope">
          <el-button @click="listen(scope.row)" size="mini" type="text"
            >试听</el-button
          >
          <el-button @click="detail(scope.row)" size="mini" type="text"
            >答案</el-button
          >
          <el-button @click="shanchu(scope.row)" size="mini" type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <el-dialog :visible.sync="audioShow">
      <!-- <myAudioVue v-if="audioShow" :path="path"></myAudioVue> -->
      <vue-audio v-if="audioShow" :audio-source="path"></vue-audio>
    </el-dialog>
    <el-dialog :visible.sync="answerShow">
      <listenAnswerVue
        v-if="answerShow"
        @custom-event="handleCustomEvent"
        :audio="audio"
      ></listenAnswerVue>
    </el-dialog>
  </div>
</template>

<script>
import shangChuanVue from "./shangChuan.vue";
import audioApi from "@/api/audio/index";
// import myAudioVue from "../learn/myAudio.vue";
import listenAnswerVue from "./listenAnswer.vue";
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import host from "@/utils/host";

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: { shangChuanVue, listenAnswerVue },
  props: {},
  data() {
    //这里存放数据
    return {
      path: "", //audio表的path
      form: {
        fileName: "",
      },
      audio: {},
      id: -1, //audio表的主键
      url: "",
      show: false,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      audioShow: false, //显示试听页面
      answerShow: false, //显示答案编辑页面
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    //自定义事件，用于父子通信
    handleCustomEvent(data) {
      if (data.type == "audio_updated" && data.status) {
        setTimeout(() => {
          this.answerShow = false;
        }, 1500);
      }
    },
    //查看详情
    detail: async function (params) {
      const res = await audioApi.getAudioQuestionByName(params.name);
      if (!params.questions) {
        this.audio.questions = res.data.questions;
        this.answerShow = true;
      } else {
        this.audio = params;
        this.answerShow = true;
      }
      if (!params.answers) {
        this.audio.answers = res.data.answers;
        this.answerShow = true;
      } else {
        this.audio = params;
        this.answerShow = true;
      }
      this.audio.name = params.name;
      this.audio.id = params.id;
    },
    shanchu(params) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          audioApi.deleteById(params.id).then((res) => {
            this.$message({
              message: `${res.message}`,
              type: "success",
            });
            this.list(1);
            this.getTotal();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //试听
    listen(params) {
      //todo 这里要出现一个播放器
      // this.path = params.path;
      this.audioShow = true;
      this.path = "http://" + host + ":8080/audio/" + params.path;
    },
    search() {
      this.list(this.currentPage);
    },
    shangChuanEvent(payload) {
      if (payload == "success") {
        this.show = false;
        this.list(this.currentPage);
        this.getTotal();
      }
    },
    upload() {
      this.show = true;
      this.url = "http://" + host + ":8080/elearning/audio/upload";
    },
    list: async function (currentPage) {
      const res = await audioApi.getList(
        this.form.fileName,
        currentPage,
        this.pageSize
      );
      this.tableData = res.data;
    },
    getTotal: async function () {
      const res = await audioApi.getTotal(this.form.fileName);
      this.total = res.data;
    },
    handleCurrentChange: async function (currentPage) {
      this.list(currentPage);
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.list(this.currentPage);
    this.getTotal();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped></style>
